/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.pizzaria.view;

import br.com.pizzaria.bean.ClienteBean;
import br.com.pizzaria.controller.ClienteController;
import br.com.pizzaria.util.ConectaBanco;
import br.com.pizzaria.util.VerificarData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Jaime
 */
public class ClientePesquisa extends javax.swing.JDialog {

    /**
     * Creates new form ClientePesquisa
     */
    ClienteBean clienteBeans;
    ClienteController clienteController;
    DefaultTableModel modelo;
    ButtonGroup grupoPesquisa;

    public ClientePesquisa(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        clienteBeans = new ClienteBean();
        clienteController = new ClienteController();
        modelo = (DefaultTableModel) tblCliente.getModel();
        grupoPesquisa = new ButtonGroup();
        grupoPesquisa.add(rbNome);
        grupoPesquisa.add(rbTelefone);
        grupoPesquisa.add(rbEndereco);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblCliente = new javax.swing.JTable();
        rbNome = new javax.swing.JRadioButton();
        rbTelefone = new javax.swing.JRadioButton();
        rbEndereco = new javax.swing.JRadioButton();
        txtPesquisar = new javax.swing.JFormattedTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        tblCliente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome", "Rua", "Bairro", "Telefone"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblCliente.getTableHeader().setReorderingAllowed(false);
        tblCliente.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tblClienteMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(tblCliente);

        rbNome.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        rbNome.setSelected(true);
        rbNome.setText("Nome");

        rbTelefone.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        rbTelefone.setText("Telefone");

        rbEndereco.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        rbEndereco.setText("Endereço");

        txtPesquisar.setForeground(new java.awt.Color(0, 51, 255));
        txtPesquisar.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtPesquisar.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        txtPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPesquisarActionPerformed(evt);
            }
        });
        txtPesquisar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtPesquisarKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 642, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(rbNome)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(rbTelefone)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(rbEndereco)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtPesquisar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbNome)
                    .addComponent(rbTelefone)
                    .addComponent(rbEndereco)
                    .addComponent(txtPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE)
                .addGap(37, 37, 37))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public ClienteBean getClienteBean(){
        return clienteBeans;
    }
    
    
    private void tblClienteMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblClienteMousePressed
        //clienteBeans = clienteController.controlePreenchimento(Integer.parseInt(modelo.getValueAt(tblCliente.getSelectedRow(), 0).toString()));
        if (evt.getClickCount() == 2) {
            clienteBeans = clienteController.controlePreenchimento(((ClienteBean) modelo.getValueAt(tblCliente.getSelectedRow(), 0)).getCodigoCliente());
            
            this.dispose();
        }
        //txtCodigo.setText(clienteBeans.getCodigoCliente() + "");

    }//GEN-LAST:event_tblClienteMousePressed

    private void txtPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPesquisarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPesquisarActionPerformed

    private void txtPesquisarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPesquisarKeyReleased

        String tipoPesquisa;
        if (rbNome.isSelected()) {
            tipoPesquisa = "nome";
        } else if (rbTelefone.isSelected()) {
            tipoPesquisa = "telefone";
        } else {
            tipoPesquisa = "rua";
        }

        modelo.setNumRows(0);

        procuraCliente(txtPesquisar.getText(), tipoPesquisa, modelo);

//        modelo.setNumRows(0);
//        clienteController.controlePesquisa(txtPesquisar.getText(), modelo);

    }//GEN-LAST:event_txtPesquisarKeyReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ClientePesquisa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ClientePesquisa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ClientePesquisa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ClientePesquisa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ClientePesquisa dialog = new ClientePesquisa(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton rbEndereco;
    private javax.swing.JRadioButton rbNome;
    private javax.swing.JRadioButton rbTelefone;
    private javax.swing.JTable tblCliente;
    private javax.swing.JFormattedTextField txtPesquisar;
    // End of variables declaration//GEN-END:variables

    public void procuraCliente(String pesquisa, String tipo, DefaultTableModel modelo) {
        String SQLSelection;

        switch (tipo) {
            case "nome":
                SQLSelection = "select * from cliente where cli_nome like '%" + pesquisa + "%';";
                break;
            case "telefone":
                SQLSelection = "select * from cliente where cli_telefone like '%" + pesquisa + "%';";
                break;
            default:
                SQLSelection = "select * from cliente where cli_rua like '%" + pesquisa + "%';";
                break;
        }
        try (PreparedStatement pstm = ConectaBanco.getConnection().prepareStatement(SQLSelection)) {

            ResultSet rs = pstm.executeQuery();
//            if (pesquisa.equals("")) {
//                rs.next();
//            }
            while (rs.next()) {
                ClienteBean clienteBeans2 = new ClienteBean();
                clienteBeans2.setObservacao(rs.getString("cli_obs"));
                clienteBeans2.setCep(rs.getString("cli_cep"));
                clienteBeans2.setAniversario(VerificarData.converteParaJAVA(rs.getString("cli_aniversario")));
                clienteBeans2.setTelCelular(rs.getString("cli_tel_cel"));
                clienteBeans2.setEmail(rs.getString("cli_email"));
                clienteBeans2.setNumero(rs.getInt("cli_nro_ender"));
                clienteBeans2.setCodigoCliente(rs.getInt("cli_cod"));
                clienteBeans2.setNome(rs.getString("cli_nome"));
                clienteBeans2.setRua(rs.getString("cli_rua"));
                clienteBeans2.setBairro(rs.getString("cli_bairro"));
                clienteBeans2.setTelefone(rs.getString("cli_telefone"));
                clienteBeans2.setDataCadastro(VerificarData.converteParaJAVA(rs.getString("cli_datacad")));
                clienteBeans2.setCidade(rs.getString("cli_cidade"));
                modelo.addRow(new Object[]{clienteBeans2, rs.getString("cli_rua"), rs.getString("cli_bairro"), rs.getString("cli_telefone")});
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Impossível Cadastrar", "Erro de SQL", 0, new ImageIcon("imagens/cancelar.png"));
        }
    }

}
